package com.learn.demo.array;

public class MatrixRotation {
    public static void main(String[] args) {
        int[][] mat = {{1,2,3},{4,5,6},{7,8,9}};
        int n = 3;
        int[][] ints = rotateMatrix(mat, 3);
    }

    /**
     * 输入：
     * > [[1,2,3],[4,5,6],[7,8,9]],3
     *
     * 返回值：
     * >[[7,4,1],[8,5,2],[9,6,3]]
     * @param mat
     * @param n
     * @return
     */
    public static int[][] rotateMatrix(int[][] mat, int n) {
        // write code here
        int[][] result = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                result[n-j-1][i] = mat[n-i-1][j];
            }
        }
        for (int i = 0; i < n; i++) {
            if(i< n/2){
                int[] temp = result[i];
                result[i] = result[n-i-1];
                result[n-i-1] = temp;
            }
        }
        return result;
    }
}
